//
//  MDObject.m
//  midai
//
//  Created by duomai on 13-11-26.
//  Copyright (c) 2013年 tiny. All rights reserved.
//

#import "MDObject.h"
#import <objc/runtime.h>

@implementation MDObject
- (id)initWithDic:(NSDictionary *)dic
{
    
    unsigned int outCount = 0;
    Class currentClass = [self class];
    while (currentClass) {
        objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
        for (int i = 0; i < outCount; i++) {
            objc_property_t property = properties[i];;
            NSString *propertyNameString =[[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding] ;
            
            if ([propertyNameString isEqualToString:@"id_i"]) {
                propertyNameString = @"id";
            }
            
            id value = [dic objectForKey:propertyNameString];
            if (value == [NSNull null]) {
                continue;
            }
            
            if (value) {
                if ([propertyNameString isEqualToString:@"id"]) {
                    [self setValue:[dic objectForKey:@"id"] forKey:@"id_i"];
                }else{
                    [self setValue:value forKey:propertyNameString];
                }
            }
        }
        Class superclass = [currentClass superclass];
        currentClass = superclass;
    }
    return self;
}

+ (NSMutableArray *) getListArray:(NSArray *) aArray
{
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    for(NSDictionary *dic in aArray)
    {
        [dataArray addObject:[[self alloc]initWithDic:dic]];
    }
    return dataArray;
}
@end
